import * as cacheUtils from '../../../../model/CacheUtils';
import constants from '../../../../common/Constants.js';
import * as regexUtils from '../../../../utils/RegexUtils.js';
import * as netWork_utils from '../../../../http/NetworkUtils.js';
import PageRouter from '../../../../utils/PageRouter';

import {$Toptips} from '../../../../components/CompRouter';

var app = getApp();
var gAgentId = '';

Page({

    /**
     * 页面的初始数据
     */
    data: {
        cacheManager: null,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //获取代理商Id
        this.getAgentId();

        let cacheManager = new cacheUtils.cacheManager()
            .init("agentOpenAcc", constants.STORAGE_FUNC.AGENT_OPEN_ACC);
        let agentBasiInfo = cacheManager.getFunc()
        _pushDataToArr(this, agentBasiInfo)
        this.setData({
            cacheManager: cacheManager
        })
    },

    agentInputEvent: function (e) {
        let paramsKey = e.currentTarget.dataset.current
        this.data.cacheManager.cacheFunc({[paramsKey]: e.detail.value});
    },

    accInfoSubmit: function (e) {
        let formValues = e.detail.value;
        let errorMsg = this._checkForm(formValues);
        if (errorMsg) {
            $Toptips.error({text: errorMsg});
        } else {
            this._postAccInfo(formValues)
        }
    },

    /**
     * 获取代理商编号
     */
    getAgentId () {
        if (app.globalData.userCache['newAgentId']) {
            gAgentId = app.globalData.userCache['newAgentId'];
        } else {
            let params = {
                loginAgentLevel: app.globalData.userData.agentLevel
            };
            netWork_utils.buildRequest(constants.Interfaces.FUNC_GET_AGENTID, params, {
                doHttpSucess: (response) => {
                    if (constants.RetCodes.SUCCESS === response.retCode) {
                        gAgentId = response['agentId'];
                        cacheUtils.saveUserCache({newAgentId: response['agentId']});
                        return true;
                    }
                    return false;
                }
            }).send();
        }
    },

    _postAccInfo: function (formValues) {
        formValues["agentId"] = gAgentId;
        netWork_utils.buildRequest(constants.Interfaces.FUNC_OPEN_AGENT, formValues, {
            doHttpSucess: (data) => {
                if (constants.RetCodes.SUCCESS === data.retCode) {
                    this.data.cacheManager.clearFunc();
                    cacheUtils.saveUserCache({newAgentId: ""});

                    let navParams = {
                        assistId: formValues.assistId,
                        userId: "admin",
                        password: "111111"
                    };
                    PageRouter.wxNavigateTo("openAgent", "../../../basisInfo/auditResult/auditResult", true, navParams);
                    // wx.redirectTo({
                    //     url: '../../../basisInfo/auditResult/auditResult?flag=openAgent',
                    // });
                    return true
                }
                return false
            }
        }).send();
    },

    _checkForm(formValues) {
        if (!formValues.agentName) {
            return "请输入正确的代理商全称";
        }
        if (!formValues.agentShortName) {
            return "请输入正确的代理商简称";
        }

        let values = [
            [formValues.registrationNo, constants.RegexTypes.REGISTRATIONNO],
            [formValues.busiRegistNum, constants.RegexTypes.REGISTRATIONNO],
            [formValues.userMobile, constants.RegexTypes.MOBILE],
            // [formValues.assistId, constants.RegexTypes.MOBILE],
            [formValues.email, constants.RegexTypes.LINKTELEMAIL],
        ];

        let result = regexUtils.regexTypesWithMsg(values);
        if (result) {
            return result;
        }
        result = regexUtils.regexTypeWithMsg(formValues.assistId, constants.RegexTypes.MOBILE);
        if (result) {
            return "登录账号必须是管理员手机号";
        }
        return '';
    },
});

function _pushDataToArr(page, result) {
    let arr = [];
    arr.push({
        itemTitle: "代理商全称", itemName: "agentName",
        typeStr: "text", placeholderStr: "请输入代理商全称",
        content: result ? result["agentName"] : ''
    });
    arr.push({
        itemTitle: "代理商简称", itemName: "agentShortName",
        typeStr: "text", placeholderStr: "请输入代理商简称",
        content: result ? result["agentShortName"] : ''
    });
    arr.push({
        itemTitle: "组织机构码", itemName: "registrationNo",
        typeStr: "text", placeholderStr: "请输入组织机构码",
        content: result ? result["registrationNo"] : ''
    });
    arr.push({
        itemTitle: "工商登记号", itemName: "busiRegistNum",
        typeStr: "text", placeholderStr: "请输入工商登记号",
        content: result ? result["busiRegistNum"] : ''
    });
    arr.push({
        itemTitle: "联系人", itemName: "userName",
        typeStr: "text", placeholderStr: "请输入联系人姓名",
        content: result ? result["userName"] : ''
    })
    arr.push({
        itemTitle: "联系人手机", itemName: "userMobile",
        typeStr: "number", placeholderStr: "请输入联系人手机号",
        content: result ? result["userMobile"] : ''
    })
    page.setData({
        agentBasiInfo: arr,
        assistId: result ? result["assistId"] : '',
        email: result ? result["email"] : ''
    })
}